#!/bin/bash

set -x
set -euo pipefail

SCRIPT_DIR=$(dirname $(readlink -f $0))

source /etc/os-release
PLATFORM=${ID#rh*}${VERSION_ID%.*}

function build_install_asn1c_oai {
    rm -rf /tmp/asn1c_oai
    git clone https://gitlab.eurecom.fr/oai/asn1c.git /tmp/asn1c_oai
    cd /tmp/asn1c_oai
    git checkout 0a7524184f16e7093990a31d8d4db487a16e5782
    autoreconf -iv
    ./configure --program-suffix=_oai
    make -j`nproc`
    make install
    mv /usr/local/share/asn1c /usr/local/share/asn1c_oai
}

function build_install_asn1c_cn {
    rm -rf /tmp/asn1c_cn
    git clone https://gitlab.eurecom.fr/oai/asn1c.git /tmp/asn1c_cn
    cd /tmp/asn1c_cn
    ./configure --program-suffix=_cn
    make -j`nproc`
    make install
    mv /usr/local/share/asn1c /usr/local/share/asn1c_cn
}

function build_install_uhd {
    rm -rf /tmp/uhd
    git clone https://github.com/EttusResearch/uhd.git /tmp/uhd
    cd /tmp/uhd
    git checkout tags/v3.13.1.0
    mkdir -p host/build
    cd host/build
    cmake ../
    make -j`nproc`
    make test
    make install
    ldconfig
}

function build_install_freediameter {
    rm -rf /tmp/freediameter
    GIT_SSL_NO_VERIFY=true git clone --branch=eurecom-1.2.0 https://gitlab.eurecom.fr/oai/freediameter.git /tmp/freediameter
    cd /tmp/freediameter
    patch -p1 < ${SCRIPT_DIR}/../patches/freediameter_eurecom-1.2.0_0001-S6a-dictionary-add-CLR-Flags.patch
    patch -p1 < ${SCRIPT_DIR}/../patches/freediameter_compatibility-1.2.0_patch_0001-S6a-dictionary-avp493.patch
    patch -p1 < ${SCRIPT_DIR}/../patches/freediameter-1.x_api-fix.patch
    mkdir -p build
    cd build
    cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr/local ../
    make -j`nproc`
    make install
}

function build_install_liblfds {
    rm -rf /tmp/liblfds*
    git clone https://github.com/liblfds/liblfds.git /tmp/liblfds
    cd /tmp/liblfds/liblfds/liblfds7.1.0/liblfds710/build/gcc_gnumake
    make so_dbg
    make so_install
}

function build_install_double-conversion {
    rm -rf /tmp/double-conversion
    git clone --depth=1 --branch=v3.1.5 https://github.com/google/double-conversion.git /tmp/double-conversion
    cd /tmp/double-conversion
    cmake .
    make -j`nproc`
    make install
}

function build_install_folly {
    rm -rf /tmp/folly
    git clone --depth=1 --branch=v2019.06.17.00 https://github.com/facebook/folly.git /tmp/folly
    git clone --depth=1 --branch=release-1.8.0 https://github.com/google/googletest.git /tmp/folly/folly/test/gtest
    cd /tmp/folly
    patch -p1 < ${SCRIPT_DIR}/../patches/folly-gflagslib-fix.patch
    mkdir -p _build
    cd _build
    cmake ..
    make -j`nproc`
    make install
}

function build_install_libuv {
    rm -rf /tmp/libuv
    git clone --depth=1 --branch=v1.23.1 https://github.com/libuv/libuv.git /tmp/libuv
    cd /tmp/libuv
    mkdir -p build
    cd build
    cmake ..
    make -j`nproc`
    make install
}

function build_install_protobuf {
    rm -rf /tmp/protobuf
    git clone --depth=1 --branch=v3.3.0 https://github.com/protocolbuffers/protobuf.git /tmp/protobuf
    cd /tmp/protobuf
    git submodule update --init --recursive
    ./autogen.sh
    ./configure
    make -j`nproc`
    make install
}

function build_install_protobuf_c {
    export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
    rm -rf /tmp/protobuf-c
    git clone --depth=1 --branch=v1.3.0 https://github.com/protobuf-c/protobuf-c.git /tmp/protobuf-c
    cd /tmp/protobuf-c
    ./autogen.sh
    ./configure
    make -j`nproc`
    make install
}

case "${PLATFORM}" in
    el8)
        (cd /usr/bin && ln -sf python2 python)
        ;;
    el7)
        (cd /usr/bin && ln -sf cmake3 cmake)
        build_install_protobuf
        build_install_protobuf_c
        ;;
esac
build_install_asn1c_oai
build_install_asn1c_cn
build_install_uhd
uhd_images_downloader
build_install_freediameter
build_install_liblfds
build_install_double-conversion
build_install_folly
build_install_libuv
